﻿//using Microsoft.Extensions.Configuration;
//using Microsoft.Extensions.Hosting;
//using Microsoft.Extensions.Logging;
//using Newtonsoft.Json;
//using SHKF.B2C.JuShuiTan.Model;
//using SHKF.B2C.JuShuiTan.Model.Inter.OrdersQuery;
//using SHKF.B2C.JuShuiTan.Network;
//using SHKF.B2C.Model;
//using SHKF.B2C.Model.Entity.Order;
//using System;
//using System.Collections.Generic;
//using System.Diagnostics;
//using System.Linq;
//using System.Threading;
//using System.Threading.Tasks;

//namespace SHKF.B2C.JuShuiTan.Service
//{
//    public class JobHostedService : IHostedService
//    {
//        private readonly IConfiguration configuration;
//        private readonly ILogger<JobHostedService> logger;
//        private readonly JSTHttpClient httpClient; 
//        private Thread thread;
         
//        public JobHostedService(IConfiguration configuration,
//            ILogger<JobHostedService> logger,
//            JSTHttpClient httpClient)
//        {
//            this.configuration = configuration;
//            this.logger = logger;
//            this.httpClient = httpClient; 
//        }


//        public Task StartAsync(CancellationToken cancellationToken)
//        {
//            return Task.Factory.StartNew(() =>
//            {

//                if (thread == null)
//                {
//                    thread = new Thread(new ParameterizedThreadStart((object t) =>
//                    {

//                        while (true)
//                        {
//                            OrdersSingleParam singleParam = new OrdersSingleParam();
//                            singleParam.page_index = 1;
//                            singleParam.page_size = 50;

//                            singleParam.shop_id = 0;

//                            DateTime dtStart = DateTime.Now;
//                            DateTime dtEnd = dtStart.AddDays(7);

//                            singleParam.modified_begin = dtStart.ToString("yyyy-MM-dd HH:mm:ss");
//                            singleParam.modified_end = dtEnd.ToString("yyyy-MM-dd HH:mm:ss");
 
//                            Thread.Sleep(10 * 1000); 
//                            doCheck(singleParam);

//                            this.logger.LogInformation($"执行获取定单  {DateTime.Now}");
//                            Thread.Sleep(3 * 1000);
//                        }
//                    }));

//                    thread.Start();
//                }

//            }, cancellationToken);
//        }

//        public Task StopAsync(CancellationToken cancellationToken)
//        {
//            return Task.Factory.StartNew(() =>
//            {
//                Process.GetCurrentProcess().Kill();

//            }, cancellationToken);
//        }

//        private void doCheck(OrdersSingleParam singleParam)
//        {
//            try
//            {

//                OrderDto dto = this.httpClient.OrderQeury(singleParam).Result;

//                if (dto.code == 0 && dto.data.data_count > 0)
//                {
//                    //解析数据
//                    doAnalysis(dto.data.orders);

//                    //保存数据

//                    //下一个
//                    if ( dto.data.has_next)
//                    {
//                        singleParam.page_index++; 
//                        this.doCheck(singleParam);
//                    } 
//                }
//                else
//                {
//                    this.logger.LogInformation(dto.msg);
//                }
//            }
//            catch (Exception ex)
//            {
//                this.logger.LogError(ex.Message);
//            }

//        }

//        private void doAnalysis(List<Order> orders)
//        {
//            foreach(Order order in orders)
//            {
//                OrderContentEntity orderInfo = CreateOrder(order);

//                List<OrderDetailEntity> detailList = new List<OrderDetailEntity>();
//                foreach (OrderItem item in order.items)
//                {
//                    OrderDetailEntity itemNew = CreateOrderDetail(item,0,0); 
//                    detailList.Add(itemNew);
//                }

               
//            }
//        }

//        private OrderContentEntity CreateOrder(Order order)
//        {
//            pays pay = order.pays[0];

//            OrderContentEntity orderEntity = new OrderContentEntity();
//            orderEntity.app_poi_code = order.shop_id;
//            orderEntity.ID = order.so_id;
//            orderEntity.status = 4;
//            orderEntity.order_id = order.o_id;
//            orderEntity.wm_order_id_view = 0;
//            orderEntity.wm_poi_name = order.shop_name;
//            orderEntity.wm_poi_address = order.shop_site;
//            orderEntity.wm_poi_phone = "";
//            orderEntity.recipient_address = order.receiver_address;
//            orderEntity.recipient_phone = order.receiver_mobile;
//            orderEntity.recipient_name ="";
         
//            orderEntity.total = order.paid_amount;
//            orderEntity.total_fee = order.pay_amount;
//            orderEntity.caution = order.remark;
//            orderEntity.shipper_phone = ""; 
//            orderEntity.has_invoiced = 1;
//            orderEntity.invoice_title = order.invoice_title;
//            orderEntity.Platform = PlatformEnum.YuShuiTan;
//            orderEntity.ctime = DateTime.Parse( order.created );
//            orderEntity.utime = DateTime.Now;
           

//            if (order.plan_delivery_date == "")
//            {
//                orderEntity.delivery_time = new DateTime(1900, 1, 1);
//            }
//            else
//            {
//                orderEntity.delivery_time = DateTime.Parse( order.plan_delivery_date );
//            }

//            orderEntity.day_seq = 0;

//            orderEntity.total_weight = order.f_weight;
//            return orderEntity;
//        }

//        private OrderDetailEntity CreateOrderDetail(OrderItem item, long order_id, long app_poi_code)
//        {

//            OrderDetailEntity orderDetailEntity = new OrderDetailEntity();

//            orderDetailEntity.FKSN = order_id;
//            orderDetailEntity.actual_price = 0;
//            orderDetailEntity.original_price = 0;
//            orderDetailEntity.app_medicine_code = "";
//            orderDetailEntity.product_name = item.name;
//            orderDetailEntity.upc = "";
//            orderDetailEntity.qty = item.qty;
//            orderDetailEntity.price = item.price;
//            orderDetailEntity.box_num = 0;
//            orderDetailEntity.box_price = 0;
//            orderDetailEntity.unit = "";
//            orderDetailEntity.food_discount = 0;
//            orderDetailEntity.weight = 0;
//            orderDetailEntity.weight_unit = "";

//            //orderDetailEntity.ProductSN = 0;
            
//            return orderDetailEntity;
//        }


//        private void doSave(List<Order> orders)
//        {
            
//        }
//    }
//}
